package routers

import (
	"gin-wheel/app/middleware"
	v1 "gin-wheel/app/routers/api/v1"
	"gin-wheel/global"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

func NewRouter() *gin.Engine {

	gin.SetMode(global.ServerSetting.RunMode)
	r := gin.New()

	// debug模式下使用pprof
	if global.ServerSetting.RunMode == "debug" {
		pprof.Register(r)
	}

	// 如果使用了nginx基本不需要accesslog中间件 nginx日志比这个更详细
	r.Use(middleware.AccessLog(), middleware.Recovery())

	// 允许跨域
	if global.ServerSetting.AllowCrossDomain {
		r.Use(middleware.Core())
	}
	// r.Use(middleware.JWT())

	apiv1 := r.Group("/api/v1")
	{
		// 用户注册登录token相关
		user := v1.NewUser()
		apiuser := apiv1.Group("/user")
		{
			apiuser.POST("/register", user.Register)
			apiuser.POST("/login", user.Login)
			apiuser.POST("/refresh_token", user.RefreshToken)
			apiuser.GET("/testredis", user.TestRedis)
		}

		// 验证码相关
		captcha := v1.NewCaptcha()
		apicaptcha := apiv1.Group("/captcha")
		{
			apicaptcha.GET("/generate", captcha.GenerateId)
			apicaptcha.GET("/:captcha_id", captcha.Img)
			apicaptcha.GET("/:captcha_id/:value", captcha.CheckCode)
		}

		// 雪花算法生成ID测试
		snowflakeRouter := v1.NewSnowflakeRouter()
		apisnowflake := apiv1.Group("/snowflake")
		apisnowflake.Use(middleware.JWT())
		apisnowflake.GET("/generate", snowflakeRouter.GenerateIds)
	}
	return r
}
